// $(document).ready(function () {
//     getArray();
//     $("#submit").on("click", function () {
//         getArray(1);
//     })
// })

function getArrayByParam(pageNum, role, name, department) {
    var data = "pageNum=" + pageNum + "&pageSize=10";
    if (role != null && role != "null") {
        data += "&role=" + role
    }
    if (name != null && name != "null") {
        data += "&name=" + name
    }
    if (department != null && department != "null") {
        data += "&departmentId=" + department;
    }
    $.ajax({
        "url": url + userArrayUrl,
        "type": 'get',
        "data": data,
        "dataType": "json",
        "success": function (data) {
            var res = eval('(' + JSON.stringify(data) + ')');
            if (res.resultCode != 200) {
                error(data);
                return;
            }
            var obj = JSON.stringify(res.obj);

            var user = localStorage.getItem(schoolLoginUserInfo);
            if (user == null) {
                toLogin();
            }
            var res = eval('(' + user + ')');
            if (res.role == 4) {
                sysUserAppand(obj);
            } else {
                userAppand(obj);
            }
            page(obj);
            saveParam(role, name, department);//用于翻页，保存好这些数据
        },
        "error": error()
    })
}


function saveParam(role, name, department) {
    localStorage.setItem(findRole, role);
    localStorage.setItem(nameLike, name);
    localStorage.setItem(findDepartment, department);
};

function getArray(pageNum) {
    var role = localStorage.getItem(findRole);
    var name = localStorage.getItem(nameLike);
    var department = localStorage.getItem(findDepartment);
    getArrayByParam(pageNum, role, name, department);
}

function userAppand(data) {
    var page = eval('(' + data + ')');
    var array = page.obj;
    var table = "<tr>\n" +
        "                        <th>序号</th>\n" +
        "                        <th>姓名</th>\n" +
        "                        <th>性别</th>\n" +
        "                        <th>电话</th>\n" +
        "                        <th>入校时间</th>\n" +
        "                        <th>学系</th>\n" +
        "                        <th>角色</th>\n" +
        "                        <th>职称</th>\n" +
        // "                        <th>学生评分</th>\n" +
        // "                        <th>同行评分</th>\n" +
        // "                        <th>专家评分</th>\n" +
        "                        <th>操作</th>\n" +
        "                    </tr>";
    for (var i = 0; i < array.length; i++) {
        table += "<tr>\n" +
            "                        <td>" + array[i].id + "</td>\n" +
            "                        <td>" + array[i].name + "</td>\n";
        if (array[i].sex == 1) {
            table += "<td>" + "男" + "</td>\n";
        } else {
            table += "<td>" + "女" + "</td>\n";
        }
        table += "                        <td>" + array[i].phone + "</td>\n" +
            "                        <td>" + timestampToTime(array[i].inSchoolTime) + "</td>\n" +
            "                        <td>" + array[i].departmentName + "</td>\n";
        if (array[i].role == 1) {
            table +=
                "                        <td>" + "学生" + "</td>\n";
        } else if (array[i].role == 2) {
            table +=
                "                        <td>" + "教师" + "</td>\n";
        } else if (array[i].role == 3) {
            table +=
                "                        <td>" + "专家" + "</td>\n";
        }

        // "                        <td>" + array[i].studentScore + "</td>\n" +
        // "                        <td>" + array[i].teacherScore + "</td>\n" +
        // "                        <td>" + array[i].expertScore + "</td>\n" +
        table +=
            "                      <td>" + array[i].title + "</td>\n" +
            "                        <td >" + "<a href='#' onClick='score(" + array[i].id + "," + array[i].departmentId + ")'>评论</a>" +
            "|" + "<a href='#' onClick='seeInfo(" + array[i].id + ")'>查看</a>" + "</td>\n";
        table += "                    </tr>";
    }
    $("#userTable").empty()
    $("#userTable").append(table);
}

function sysUserAppand(data) {
    var page = eval('(' + data + ')');
    var array = page.obj;
    var table = "<tr>\n" +
        "                        <th>序号</th>\n" +
        "                        <th>姓名</th>\n" +
        "                        <th>性别</th>\n" +
        "                        <th>电话</th>\n" +
        "                        <th>入校时间</th>\n" +
        "                        <th>学系</th>\n" +
        "                        <th>角色</th>\n" +
        "                        <th>操作</th>\n" +
        "                    </tr>";
    for (var i = 0; i < array.length; i++) {
        table += "<tr>\n" +
            "                        <td>" + array[i].id + "</td>\n" +
            "                        <td>" + array[i].name + "</td>\n";
        if (array[i].sex == 1) {
            table += "<td>" + "男" + "</td>\n";
        } else {
            table += "<td>" + "女" + "</td>\n";
        }
        table += "                        <td>" + array[i].phone + "</td>\n" +
            "                        <td>" + timestampToTime(array[i].inSchoolTime) + "</td>\n" +
            "                        <td>" + array[i].departmentName + "</td>\n";
        if (array[i].role == 1) {
            table +=
                "                        <td>" + "学生" + "</td>\n";
        } else if (array[i].role == 2) {
            table +=
                "                        <td>" + "教师" + "</td>\n";
        } else if (array[i].role == 3) {
            table +=
                "                        <td>" + "专家" + "</td>\n";
        }
        table += "<td >" + "<a href='#' onClick='changeWindow(" + array[i].id + ")'>修改</a>" + "|" +
            "<a href='#' onClick='deleteData(" + array[i].id + ")'>删除</a>"
            + "</td>\n";
        table += "</tr>";
    }
    $("#userTable").empty()
    $("#userTable").append(table);
}


function score(tId, departmentId) {
    var user = localStorage.getItem(schoolLoginUserInfo);
    if (user == null) {
        toLogin();
    }
    var res = eval('(' + user + ')');
    var data = "tId=" + tId + "&commentatorId=" + res.id;
    if(res.role == 1){
        var teachers=res.classTeacherId;
        var teacherId="";
        if(teachers.indexOf(".")==-1){
            teacherId.add(teachers);
        }else{
            var right=teachers.substring(teachers.indexOf(".")+1,teachers.length);
            var left=right.substring(0,right.lastIndexOf("."));
            teacherId=left.split("..");

        }
        var is=false;
        for(var s=0;s<teacherId.length;s++){
            if(teacherId[s]==tId){
                is=true;
            }
        }
        if(!is){
            alert("只能评论自己的任课教师哦！");//学生只能评论自己的任课教师。
            return;
        }
    }
    if ((res.role==2) && departmentId != res.departmentId) {
        alert("只能评论自己系的教师哦！")
        return;
    }
    $.ajax({
        "url": url + scoreUrl,
        "type": 'get',
        "data": data,
        "dataType": "json",
        "success": function (data) {
            var res = eval('(' + JSON.stringify(data) + ')');
            if (res.resultCode != 200) {
                error(data);
                return;
            }
            var obj = JSON.stringify(res.obj);
            if (obj == null || obj == "null") {
                openWindow(tId)
            } else {
                alert("只能评论一次哦！")
            }
        },
        "error": error()
    })
}

function openWindow(tId) {
    window.open('score.html?tId=' + tId, 'new', 'location=no, toolbar=no');
    return false;
}

function changeWindow(id) {
    window.open('p1.html?id=' + id, 'new', 'location=no, toolbar=no');
    return false;
}

function deleteData(data) {
    if (!confirm("你确定删除吗？")) {
        return false;
    }
    $.ajax({
        "url": url + deleteUserUrl + data,
        "type": 'post',
        "data": data,
        "dataType": "json",
        "success": function (data) {
            var res = eval('(' + JSON.stringify(data) + ')');
            if (res.resultCode != 200) {
                error(data);
                return;
            }
            alert("删除成功！")
            location.reload([bForceGet]);
        },
        "error": error()
    })
}

function seeInfo(data){
    localStorage.setItem(userInfoId,data);
    window.location.href="userInfo.html";
}



